// import crypto from '../node_modules/crypto-js/crypto-js.js' //这种方式导入也可以
import crypto from "crypto-js";

module.exports = {
	/**
	 * 随机生成32位的字符串
	 * @returns {string}
	 */
	generateRandomString: () => {
		const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
		let result = '';
		const charactersLength = characters.length;
		for (let i = 0; i < 32; i++) {
			result += characters.charAt(Math.floor(Math.random() * charactersLength));
		}
		return result;
	},
	/**
	 * 随机生成aes 密钥
	 * @returns {string}
	 */
	generateAesKey: () => {
		const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
		let result = '';
		const charactersLength = characters.length;
		for (let i = 0; i < 32; i++) {
			result += characters.charAt(Math.floor(Math.random() * charactersLength));
		}
		console.log('result',result)
		// console.log(crypto.enc.Utf8.parse(result))
		return result;
	},
	/**
	 * 使用密钥对数据进行加密
	 * @param message
	 * @param aesKey
	 * @returns {string}
	 */
	encryptWithAes: (message, aesKey) => {
		const encrypted = crypto.AES.encrypt(message, aesKey, {
			mode: crypto.mode.ECB,
			padding: crypto.pad.Pkcs7
		});
		console.log(encrypted.toString())
		return encrypted.toString();
	},

	/**
	 * 使用密钥对数据进行解密
	 * @param message
	 * @param aesKey
	 * @returns {string}
	 */
	decryptWithAes: (message, aesKey) => {
		const decrypted = crypto.AES.decrypt(message, aesKey, {
			mode: crypto.mode.ECB,
			padding: crypto.pad.Pkcs7
		});
		return decrypted.toString(crypto.enc.Utf8);
	},

	encryptHMACSHA1: (data, encryptKey) => {
		var result = crypto.HmacSHA1(crypto.enc.Utf8.parse(data), crypto.enc.Utf8.parse(encryptKey));
		return crypto.enc.Hex.stringify(result).toUpperCase();
	},
	encryptBase64: (data) => {
		return crypto.enc.Base64.stringify(crypto.enc.Utf8.parse(data));
	},
	encrySHA256: (data) => {
		return crypto.SHA256(data).toString();
	}
}